//
//  HeadInfoCell.m
//  mendianjieke
//
//  Created by 响当当 on 8/2/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "HeadInfoCell.h"

@interface HeadInfoCell()
@property (nonatomic, strong)UIImageView *bgImageView;//背景
@property (nonatomic, strong)UIImageView *headImageView;//头像
@property (nonatomic, strong)UILabel *nameLabel;//用户名称
@property (nonatomic, strong)UILabel *headLabel;//店名
@property (nonatomic, strong)UIButton *stateButton;//营业状态
@property (nonatomic, strong)UIButton *deviceStateButton;//设备状态
//@property (nonatomic, strong)UISwitch *deviceState;//设备状态
@property (nonatomic, strong)NSString *deviceStateStr;//设备状态

@end

@implementation HeadInfoCell

- (UIImageView *)bgImageView
{
    if (!_bgImageView)
    {
        _bgImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,130*kUIScaleBasePlus)];
        _bgImageView.image = [UIImage imageNamed:@"bg"];
    }
    return _bgImageView;
}

- (UIImageView *)headImageView
{
    if (!_headImageView)
    {
        _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(20*kUIScaleBasePlus,45*kUIScaleBasePlus,60*kUIScaleBasePlus,60*kUIScaleBasePlus)];
        _headImageView.backgroundColor = LCHexColor(0xFFF6EC);
        _headImageView.layer.masksToBounds = YES;
        _headImageView.layer.cornerRadius = _headImageView.frame.size.height/2;
    }
    return _headImageView;
}

- (UILabel *)nameLabel
{
    if (!_nameLabel)
    {
        _nameLabel = [Utilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(self.headImageView.frame)+16*kUIScaleBasePlus,55*kUIScaleBasePlus,120*kUIScaleBasePlus,15.5*kUIScaleBasePlus)
                                      textAligment:NSTextAlignmentLeft
                                          textClor:LCHexColor(0xFFFFFF)
                                          textFont:16*kUIScaleBasePlus];
//        _nameLabel.text = @"1234567890";
    }
    return _nameLabel;
}

- (UILabel *)headLabel
{
    if (!_headLabel)
    {
        _headLabel = [Utilties creatLabelWithFrame:CGRectMake(CGRectGetMaxX(self.headImageView.frame)+16*kUIScaleBasePlus,CGRectGetMaxY(self.nameLabel.frame)+13.5*kUIScaleBasePlus,120*kUIScaleBasePlus,11.5*kUIScaleBasePlus)
                                      textAligment:NSTextAlignmentCenter
                                          textClor:LCHexColor(0xFFFFFF)
                                          textFont:12*kUIScaleBasePlus];
        
    }
    return _headLabel;
}

- (UIButton *)stateButton
{
    if (!_stateButton)
    {
        _stateButton = [Utilties creatButtonWithFrame:CGRectMake(SCREEN_WIDTH-22.5*kUIScaleBasePlus-45*kUIScaleBasePlus,CGRectGetMaxY(self.deviceStateButton.frame)+10*kUIScaleBasePlus,50*kUIScaleBasePlus,12.5*kUIScaleBasePlus)
                                         textAligment:UIControlContentHorizontalAlignmentCenter
                                                 text:@"营业中"
                                             textClor:LCHexColor(0xFFFFFF)
                                            buttonBGC:[UIColor clearColor]
                                             textFont:13*kUIScaleBasePlus
                                               target:self
                                               method:@selector(stateClcik)];
        _stateButton.layer.masksToBounds = YES;
        _stateButton.layer.cornerRadius = 6;
        _stateButton.enabled = NO;
    }
    return _stateButton;
}


- (UIButton *)deviceStateButton
{
    if (!_deviceStateButton)
    {
       _deviceStateButton = [Utilties creatButtonWithFrame:CGRectMake(SCREEN_WIDTH-16.5*kUIScaleBasePlus-50*kUIScaleBasePlus,57*kUIScaleBasePlus,50*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                              textAligment:UIControlContentHorizontalAlignmentCenter
                                                      text:@""
                                                  textClor:[UIColor clearColor]
                                                 buttonBGC:[UIColor clearColor]
                                                  textFont:12 target:self method:@selector(valueChanged:)];
        [_deviceStateButton setImage:[UIImage imageNamed:@"shop_open"] forState:UIControlStateNormal];
        self.deviceStateButton.selected = YES;
    }
    return _deviceStateButton;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self addSubview:self.bgImageView];
        [self addSubview:self.headImageView];
        [self addSubview:self.nameLabel];
        [self addSubview:self.headLabel];
        [self addSubview:self.deviceStateButton];
        [self addSubview:self.stateButton];

    }
    return self;
}



#pragma mark - Method
- (void)stateClcik
{
    
}

- (void)valueChanged:(UIButton *)sender
{
    
    NSString *str = LCUserDefaultsGet(@"switch");
    
    NSDictionary *pramaters = @{
        @"operate" : str.integerValue == 0 ? @"1" : @"0"
                                    };
        
        WeakSelf(self)
        [infoManager getSwitchOnInfo:pramaters
                              sucess:^(id  _Nonnull acceptData) {
                                  NSNumber *num = acceptData[@"data"];
                                  LCUserDefaultsSet(num.stringValue, @"switch");
                                  LCUserDefaultsSyn;
            if (weakSelf.changeStateBlock)
            {
                weakSelf.changeStateBlock();
            }
                              }
                                fail:^(NSError * _Nullable error) {
                                    
                                }];
}

- (void)setModel:(UserInfoModel *)model
{
    _model = model;
    self.nameLabel.text = model.userName;
    [self.nameLabel sizeToFit];
    self.headLabel.text = model.shopName;
    [self.headLabel sizeToFit];

    NSString *num = model.terminalState.stringValue;
    LCUserDefaultsSet(num, @"switch");
    LCUserDefaultsSyn;
    
    if (model.terminalState.integerValue == 1)
    {
        [self.stateButton setTitle:@"营业中" forState:UIControlStateNormal];
        [self.deviceStateButton setImage:[UIImage imageNamed:@"shop_open"] forState:UIControlStateNormal];
    }
    else
    {
        [self.stateButton setTitle:@"休息中" forState:UIControlStateNormal];
        [self.deviceStateButton setImage:[UIImage imageNamed:@"shop_close"] forState:UIControlStateNormal];

    }
    
    self.headImageView.image = [UIImage imageNamed:@"normalPhoto"];
}

- (void)changeState:(NSString *)num
{
    if (num.integerValue == 1)
       {
           [self.stateButton setTitle:@"营业中" forState:UIControlStateNormal];
           [self.deviceStateButton setImage:[UIImage imageNamed:@"shop_open"] forState:UIControlStateNormal];
       }
       else
       {
           [self.stateButton setTitle:@"休息中" forState:UIControlStateNormal];
           [self.deviceStateButton setImage:[UIImage imageNamed:@"shop_close"] forState:UIControlStateNormal];

       }
       
}
@end
